<?php
/** 
 * $Source: /cvsroot/pn-commerce/pn-commerce/pntemplates/plugins/function.pncGetItem.php,v $
 * $Author: pcornelissen $
 * $Revision: 1.2 $
 * $Date: 2003/12/08 12:09:47 $
 *
 * Type: Function
 *
 * gets an item from the db and assign the item array to a given name
 * @param  ItemSKU : get the item with this sku or
 * @param          ItemID  : get the item with this id or
 * @param          random  : when set to true we get a random item
 * @param          assign  : name of variable to assign the item array to (useful if you want to show more than one random item). If assign is not set, we will use 'item'
 * @param ordernumber
 * @param assign - (optional) the variable name for the result, default= Order
 */
function smarty_function_pncGetItem($params, &$smarty)
{
    extract($params);

    if( !pnModLoad( 'pncommerce', 'user' ) )
    {
        return false;
    }

    if ( !pnModAPILoad( 'pncommerce', 'user' ) )
    {
        return false;
    }

    if( !$assign )
    {
        $assign = 'Item';
    }

    if( $ItemSKU )
    {
        $item = pnModAPIFunc( 'pncommerce', 'user', 'getItem',
                              array( 'ItemSKU' => $ItemSKU ) );
    }
    elseif( $ItemID )
    {
        $item = pnModAPIFunc( 'pncommerce', 'user', 'getItem',
                              array( 'ItemID' => $ItemID ) );
    }
    elseif ( $Random == true )
    {
        // get a random item
		// new RANDOM
		$highid = pnModAPIFunc( 'pncommerce', 'user', 'gethighidfromDB',
						        array( 'table' => 'items',
							           'indexfield' => 'ItemID'));
		if ( $highid > 1 )
		{
			do
			{
				// figure out the random item to display
				// we do this until we find a valid ID, minimum once
				$num = mt_rand( 1, $highid );
				$result = pnModAPIFunc( 'pncommerce', 'user', 'readfromDB',
							            array( 'table' => 'items',
								               'fields' => array( 'ItemSKU', 'Category' ),
								               'required' => array( 'ItemID', $num)));
				if ($result != false )
				{
					list ($sku, $cid) = $result;
    				$result2 = pnModAPIFunc( 'pncommerce', 'user', 'readfromDB',
	    					 	             array( 'table' => 'category',
							 		                'fields' => array( 'CatInvisible' ),
							 		                'required' => array( 'CategoryID', $cid ) ) );
                    if( $result2 != false)
                    {
                        list( $inv ) = $result2;
                        if( $inv == "on" )
                        {
                            $result = false;
                        }
                    }
				}
			} while ( $result == false );
            $item = pnModAPIFunc( 'pncommerce', 'user', 'getItem',
                                  array( 'ItemSKU' => $sku ) );
            if( $item <> false)
            {
                $smarty->assign( $assign, $item );
            }
		}
		else
		{
		    // we have only one item - return this :-)
            $item = pnModAPIFunc( 'pncommerce', 'user', 'getItem',
                                  array( 'ItemID' => $highid ) );
		}
    }
    else
    {
        return "*** error: no parameter set ***";
    }
    $smarty->assign( $assign, $item );
}
?>
